//
//  SharkOffViewController.m
//  MLF
//
//  Created by lanou on 15/9/9.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "SharkOffViewController.h"
#import "ShareCollectionViewController.h"
@interface SharkOffViewController ()
@property (nonatomic,strong) NSArray *personalityArray;
@property (nonatomic,strong) NSArray *targetArray;
@property (nonatomic,strong) NSArray *sceneArray;

@property (nonatomic,assign) NSString *personality;
@property (nonatomic,assign) NSString *target;
@property (nonatomic,assign) NSString *scene;
@end

@implementation SharkOffViewController

static NSString * const reuseIdentifier = @"Cell";

-(void)loadView
{

    self.sharkOffView = [[SharkOffView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.sharkOffView.backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.view = self.sharkOffView;

        
}

- (void)back
{
    [self dismissViewControllerAnimated:NO completion:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"摇一摇选礼物";
    
    self.personalityArray = [[NSArray alloc]init];
    self.targetArray = [[NSArray alloc]init];
    self.sceneArray = [[NSArray alloc]init];
    
    self.personalityArray = @[@"26",@"28",@"43",@"44",@"45",@"46",@"47"];
    self.targetArray = @[@"16",@"17",@"18",@"19",@"20",@"21",@"22"];
    self.sceneArray = @[@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37"];

   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark  实现摇一摇功能

- (BOOL)canBecomeFirstResponder
{
    return YES;
}


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];

    animation.duration = 0.8;

    // 现获取晃动的三个点的位置
    CGFloat center = self.sharkOffView.sharkLogoImageView.layer.position.x;
    CGFloat left = center - 20;
    CGFloat right = center + 20;

    // 把位置转成对象类型
    NSNumber *c = [NSNumber numberWithFloat:center];
    NSNumber *l = [NSNumber numberWithFloat:left];
    NSNumber *r = [NSNumber numberWithFloat:right];

    animation.values = @[c,l,r,l,r,l,r,l,r,l,r,l,c];

    [self.sharkOffView.sharkLogoImageView.layer addAnimation:animation forKey:@"position.x"];


    NSString *vieoString = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
    SystemSoundID soundId;

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL URLWithString:vieoString]), &soundId);
    AudioServicesPlayAlertSound(soundId);

    // 添加音频结束回调
    AudioServicesAddSystemSoundCompletion(soundId, NULL, NULL, vedioPlayEnd, NULL);
    // 震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

}

#pragma mark 播放结束的方法
static void vedioPlayEnd()
{

    
}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    int i = arc4random() % self.personalityArray.count;
    int j = arc4random() % self.targetArray.count;
    int k = arc4random() % self.sceneArray.count;
   
    
    if (i == (int)nil) {
        i = 0;
        
    }
    if (j == (int)nil) {
        j = 0;
        
    }
    if (k == (int)nil) {
        k = 0;
        
    }

    
    ShareCollectionViewController *share = [[ShareCollectionViewController alloc] initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    share.personality = self.personalityArray[i];
    share.target = self.targetArray[j];
    share.scene = self.sceneArray[k];
    
    
    NSLog(@"%@",share.personality);
    NSLog(@"%@",share.target);
    NSLog(@"%@",share.scene);

    
    [self.navigationController pushViewController:share animated:YES];
    
    
    
   
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

}


- (void)backToSharkOff
{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
